﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using UniCT.LinkShareWpf.Interface;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing;


namespace UniCT.LinkShareWpf.CommunicationChannel
{
    public class Contract : IContract
    {

        static LinkedList<Guid> netFilterList = new LinkedList<Guid>();

        
        public void SendResource(NetResource netResource)
        {
            try
            {
               BinaryFormatter bf = new BinaryFormatter();
               MemoryStream ms = new MemoryStream(netResource.ImageBuffer);
               Bitmap image = (Bitmap)bf.Deserialize(ms);
                            
            }
            catch (Exception ee)
            {
                Console.WriteLine("FALSE"+ee.Message);
            }

           
            Console.WriteLine("Server, Ricevuta risorsa da : " + netResource.Sender.ToString());
            MyTempResourcesList.Instance.AddResource(netResource);



        }




        public void SearchRequest(NetFilter netFilter)
        {

       
            if (!netFilterList.Contains(netFilter.Id))
            {
                netFilterList.AddFirst(netFilter.Id);
                Console.WriteLine("Server, ho ricevuto un netFilter con Sorgente. " + netFilter.Sender.ToString());
                Console.WriteLine("TTL: " + netFilter.TTL);
                ObservableCollection<Resource> tempRes = netFilter.filterResources(new ObservableCollection<Resource>(MyResources.Instance.Read().Resources));

                foreach (Resource res in tempRes)
                {
                    NetResource netResource = new NetResource(res);
                    netResource.Id = netFilter.Id;
                    Client clientToSender = new Client(netFilter.Sender);
                    clientToSender.SendResourceRequest(netResource);
                 }
                netFilter.TTL--;

                string IPserver = "87.12.210.38";// System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName())[3].ToString();
                //foreach (System.Net.IPAddress IP in System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName()))
                //    if (IP.ToString().Split('.')[0] == "151" || IP.ToString().Split('.')[0] == "172" || IP.ToString().Split('.')[0] == "79" || IP.ToString().Split('.')[0] == "87")
                //    {
                //        IPserver = IP.ToString();
                //        break;
                //    }
                UriBuilder ub = new UriBuilder(Uri.UriSchemeHttp, IPserver, OperationContext.Current.Channel.LocalAddress.Uri.Port, "LinkShareWpf");



                netFilter.getNodes.AddFirst(ub.Uri);
                if (netFilter.TTL > 0)
                {

                    foreach (Node node in MyNode.Read().Nodes)
                    {
                        
                        if (!netFilter.getNodes.Contains(node.Uri) && node.Uri!=netFilter.Sender)
                        {
                            Client client = new Client(node.Uri);
                            client.SingleRequest(netFilter);
                        }
                    }

                }
            }
            else
                Console.WriteLine("Server, Ho rigettato una ricerca gia consumata");
        }




        public long Ping(long tick)
        {
            Console.WriteLine("Server, ho ricevuto un ping.");
            return tick;
        }
    }


}
